package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dao.AppointDAO;
import com.dao.SignUpDAO;
import com.dao.UserDAO;
import com.model._do.EquipmentDO;
import com.model._do.SignUpDO;
import com.model._do.UserDO;
import com.model.dto.SignedDevidedDTO;
import com.mysql.cj.util.StringUtils;
import com.service.EquipmentService;
import com.service.SignUpService;
import com.util.LocationUtil;
import com.util.PingYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("signUpService")
public class SignUpServiceImpl implements SignUpService {

    @Autowired
    private SignUpDAO signUpDAO;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private AppointDAO appointDAO;
    @Override
    public List<SignedDevidedDTO> getSignedDevided(SignUpDO sign) {
        List<EquipmentDO> equipList = equipmentService.getSignedWithMe(sign);
        HashMap<Character, SignedDevidedDTO> map = new HashMap<>();
        for (EquipmentDO equip: equipList
             ) {
            String pinyin = PingYinUtil.getFirstSpell(equip.getEquipUserName());
            char first = pinyin.charAt(0);
            if(map.get(pinyin.charAt(0)) == null){
                SignedDevidedDTO newDevide = new SignedDevidedDTO();
                newDevide.setFirstSpell(first);
                List<EquipmentDO> newList = new ArrayList<>();
                newList.add(equip);
                newDevide.setSignedList(newList);
                map.put(first, newDevide);
            } else {
                SignedDevidedDTO devide = map.get(first);
                List<EquipmentDO> oldList = devide.getSignedList();
                oldList.add(equip);
                devide.setSignedList(oldList);
            }
        }
        return map.keySet().stream().map(map::get).collect(Collectors.toList());
    }

    @Override
    public List<SignUpDO> getSignedByEquipId(SignUpDO sign) {
        return signUpDAO.getSignedByEquipId(sign.getEquipId());
    }

    @Override
    public List<SignUpDO> getSignedByUserId(SignUpDO sign) {
        return signUpDAO.getByUserId(sign);
    }

    @Override
    public SignUpDO getSignedByUserEquip(String userId, String equipId) {
        return signUpDAO.getSignedByUserEquip(userId, equipId);
    }

    @Override
    public boolean doConfirmSign(SignUpDO sign) {
        try {
            signUpDAO.doConfirmSign(sign);
            return true;
        } catch (Exception e){
            return false;
        }
    }

    @Override
    public void cancelAddNewSign(SignUpDO sign) {
        signUpDAO.cancelAddNewSign(sign);
    }

    @Override
    public void deleteSignUp(SignUpDO sign) {
        SignUpDO old = signUpDAO.getSignById(sign);
        if(old == null){
            old = signUpDAO.getSignedByUserEquip(sign.getUserId(), sign.getEquipId());
        }
        if(!StringUtils.isNullOrEmpty(sign.getEquipId())){
            old.setPatientConfirm("disagree");
        }
        if(!StringUtils.isNullOrEmpty(sign.getUserId())){
            old.setDoctorConfirm("disagree");
        }
        signUpDAO.deleteSignUp(old);
    }
}
